import {
  executeDeleteExerciseRecord,
  executeDeleteFoodRecord,
  executeExerciseRecord,
  executeFoodRecord,
  executeWeightRecord,
  queryExerciseRecordByDate,
  queryExerciseRecordByDateList,
  queryFoodRecordByDate,
  queryUserExerciseRecord,
  queryUserFoodRecord,
  queryWeightRecordByDate,
} from '@/AGC/manager/record'
import type {
  CaloriesRecordI,
  ExerciseRecordI,
  FoodRecordI,
  NutritionRecordI,
  WeightRecordI,
} from '@/types/record'
import { FoodRecord } from '@/AGC/model/FoodRecord'
import { ExerciseRecord } from '@/AGC/model/ExerciseRecord'
import { WeightRecord } from '@/AGC/model/WeightRecord'
import { getWeekByDate } from '@/utils'

/**
 * 获取用户计划天数
 * @returns 计划天数
 */
export const getUserPlanDaysUtil = async () => {
  const recordFoodList = await queryUserFoodRecord()
  const recordExerciseList = await queryUserExerciseRecord()

  let foodTotal: number = 0
  let foodDate: string = ''

  recordFoodList.forEach((record) => {
    if (foodDate != record.getDate()) {
      foodTotal += 1
      foodDate = record.getDate()
    }
  })

  let exerciseTotal: number = 0
  let exerciseDate: string = ''

  recordExerciseList.forEach((record) => {
    if (exerciseDate != record.getDate()) {
      exerciseTotal += 1
      exerciseDate = record.getDate()
    }
  })

  return foodTotal > exerciseTotal ? foodTotal : exerciseTotal
}

/**
 * 创建体重记录
 * @param record 体重记录
 */
export const createWeightRecordUtil = async (record: WeightRecordI) => {
  const weightRecord = new WeightRecord()

  if (record.id) weightRecord.setId(record.id)

  weightRecord.setUser_id(record.user_id)
  weightRecord.setDate(record.date)
  weightRecord.setWeight(record.weight)

  await executeWeightRecord(weightRecord)
}

/**
 * 根据月份查询用户的体重记录
 * @param month 月份
 * @returns 体重记录
 */
export const getWeightRecordByMonthUtil = async (month: string) => {
  // 1. 查询体重记录
  const recordList = await queryWeightRecordByDate(month)
  // 2. 返回体重记录
  return recordList.map((record) => {
    return {
      id: record.getId(),
      user_id: record.getUser_id(),
      date: record.getDate(),
      weight: record.getWeight(),
    } as WeightRecordI
  })
}

/**
 * 删除运动记录
 * @param record 运动记录
 */
export const deleteExerciseRecordUtil = async (record: ExerciseRecordI) => {
  const exerciseRecord = new ExerciseRecord()
  exerciseRecord.setId(record.id)
  await executeDeleteExerciseRecord(exerciseRecord)
}

/**
 * 根据日期获取运动记录
 * @param date 日期
 * @returns 运动记录
 */
export const getExerciseRecordByDateUtil = async (date: string) => {
  // 1. 查询运动记录
  const recordList = await queryExerciseRecordByDate(date)
  // 2. 返回运动记录
  return recordList.map((record) => {
    return {
      id: record.getId(),
      user_id: record.getUser_id(),
      date: record.getDate(),
      exercise_name: record.getExercise_name(),
      amount_unit: record.getAmount_unit(),
      calories: record.getCalories(),
    } as ExerciseRecordI
  })
}

/**
 * 根据日期列表获取运动记录
 * @param date 日期
 * @returns 运动记录
 */
export const getExericseRecordByDateListUtil = async (date: string) => {
  // 1. 获取一周的日期
  const dateList = getWeekByDate(date)
  // 2. 查询一周的运动记录
  const recordList = await queryExerciseRecordByDateList(dateList)
  // 3. 返回运动记录
  return recordList.map((record) => {
    return {
      id: record.getId(),
      user_id: record.getUser_id(),
      date: record.getDate(),
      exercise_name: record.getExercise_name(),
      amount_unit: record.getAmount_unit(),
      calories: record.getCalories(),
    } as ExerciseRecordI
  })
}

/**
 * 创建运动记录
 * @param record 运动记录
 */
export const createExerciseRecordUtil = async (record: ExerciseRecordI) => {
  const exerciseRecord = new ExerciseRecord()

  if (record.id) exerciseRecord.setId(record.id)

  exerciseRecord.setUser_id(record.user_id)
  exerciseRecord.setDate(record.date)
  exerciseRecord.setExercise_name(record.exercise_name)
  exerciseRecord.setAmount_unit(record.amount_unit)
  exerciseRecord.setCalories(record.calories)

  await executeExerciseRecord(exerciseRecord)
}

/**
 * 删除食物记录
 * @param record 食物记录
 */
export const deleteFoodRecordUtil = async (record: FoodRecordI) => {
  const foodRecord = new FoodRecord()
  foodRecord.setId(record.id)
  await executeDeleteFoodRecord(foodRecord)
}

/**
 * 根据日期获取食物记录
 * @param date 日期
 * @returns 食物记录
 */
export const getFoodRecordByDateUtil = async (date: string) => {
  // 1. 查询食物记录
  const recordList = await queryFoodRecordByDate(date)
  // 2. 返回食物记录
  return recordList.map((record) => {
    return {
      id: record.getId(),
      user_id: record.getUser_id(),
      date: record.getDate(),
      protein: record.getProtein(),
      fat: record.getFat(),
      carbohydrate: record.getCarbohydrate(),
      food_name: record.getFood_name(),
      weight_unit: record.getWeight_unit(),
      calories: record.getCalories(),
    } as FoodRecordI
  })
}

/**
 * 创建食物记录
 * @param record 食物记录
 */
export const createFoodRecordUtil = async (record: FoodRecordI) => {
  const foodRecord = new FoodRecord()

  if (record.id) foodRecord.setId(record.id)

  foodRecord.setUser_id(record.user_id)
  foodRecord.setDate(record.date)
  foodRecord.setProtein(record.protein)
  foodRecord.setFat(record.fat)
  foodRecord.setCarbohydrate(record.carbohydrate)
  foodRecord.setFood_name(record.food_name)
  foodRecord.setWeight_unit(record.weight_unit)
  foodRecord.setCalories(record.calories)

  await executeFoodRecord(foodRecord)
}

/**
 * 根据日期获取卡路里记录
 * @param date 日期
 * @returns 卡路里记录
 */
export const getCaloriesRecordUtil = async (date: string) => {
  // 1. 查询食物记录
  const foodRecord = await queryFoodRecordByDate(date)
  // 2. 查询运动记录
  const exerciseRecord = await queryExerciseRecordByDate(date)
  // 3. 计算卡路里
  const ingestion = foodRecord.reduce((acc, curr) => acc + curr.getCalories(), 0)
  const consume = exerciseRecord.reduce((acc, curr) => acc + curr.getCalories(), 0)
  // 4. 返回卡路里记录
  return { ingestion, consume } as CaloriesRecordI
}

/**
 * 根据日期获取营养成分记录
 * @param date 日期
 * @returns 营养成分记录
 */
export const getNutritionRecordUtil = async (date: string) => {
  // 1. 查询食物记录
  const foodRecord = await queryFoodRecordByDate(date)
  // 2. 计算营养成分
  const protein = foodRecord.reduce((acc, curr) => acc + curr.getProtein(), 0)
  const fat = foodRecord.reduce((acc, curr) => acc + curr.getFat(), 0)
  const carbohydrate = foodRecord.reduce((acc, curr) => acc + curr.getCarbohydrate(), 0)
  // 3. 返回营养成分记录
  return { protein, fat, carbohydrate } as NutritionRecordI
}
